package com.souku.finance.application.ui.lock;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.souku.finance.application.R;
import com.souku.finance.application.ui.login.LoginActivity;
import com.souku.finance.application.base.ActivityManage;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.utils.Constant;
import com.souku.finance.application.dialog.SimpleDialog;
import com.souku.finance.application.utils.MyTextUtil;
import com.souku.finance.application.utils.ShareUtil;
import com.souku.finance.application.http.volley.LogoutHelper;
import com.souku.finance.application.widget.lock.LockPatternView;

import java.util.List;

public class LockActivity extends BaseActivity implements
        LockPatternView.OnPatternListener, View.OnClickListener {

    private LockPatternView lockPatternView;
    private String password;
    private CountDownTimer timer;
    private boolean isChange = false;
    private TextView tv_title;
    private TextView tv_phone;
    private Button btn_forget;
    private Button btn_login;
    private ImageView iv_back;
    private int times = 4;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityManage.finishSameLesOne(LockActivity.class);
        isChange = getIntent().getBooleanExtra("change", false);
        password = ShareUtil.getInstance().getString(Constant.GesturePass);
        setContentView(R.layout.act_please_handgesture_after_main);
        tv_title = (TextView) findViewById(R.id.tv_title);
        iv_back = (ImageView) findViewById(R.id.ox);
        iv_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        if (isChange) {
            iv_back.setVisibility(View.VISIBLE);
        }
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        btn_forget = (Button) findViewById(R.id.btn_forget);
        btn_login = (Button) findViewById(R.id.btn_login);
        if (isChange) {
            tv_title.setText("");
            tv_phone.setText("请输入原手势密码");
            btn_login.setVisibility(View.GONE);
        } else {
            tv_phone.setText("HI," + MyTextUtil.phoneToPass(ShareUtil.getInstance().getString(Constant.Mobile)));
        }
        btn_login.setOnClickListener(this);
        btn_forget.setOnClickListener(this);
        lockPatternView = (LockPatternView) findViewById(R.id.lock_pattern_first);
        lockPatternView.setOnPatternListener(this);

        timer = new CountDownTimer(1000, 500) {
            @Override
            public void onTick(long millisUntilFinished) {

            }

            @Override
            public void onFinish() {
                lockPatternView.clearPattern();
            }
        };
    }

    @Override
    public void internet() {

    }

    @Override
    public void onBackPressed() {
        if (isChange) {
            finish();
        }
    }

    @Override
    public void onPatternStart() {
        tv_title.setVisibility(View.INVISIBLE);
    }

    @Override
    public void onPatternCleared() {

    }

    @Override
    public void onPatternCellAdded(List<LockPatternView.Cell> pattern) {

    }

    @Override
    public void onPatternDetected(List<LockPatternView.Cell> pattern) {
        tv_title.setVisibility(View.VISIBLE);
        if (isChange) {
            if (toStr(pattern).length() < 4) {
                lockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
                timer.start();
                tv_title.setText("至少连接4个点，请重试");
            } else if (password.equals(toStr(pattern))) {
                Intent intent = new Intent(this, LockSetupActivity.class);
                intent.putExtra("change", true);
                startActivity(intent);
                finish();
            } else {
                lockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
                timer.start();
                if (times == 0) {
                    finish();
                } else {
                    tv_title.setText("密码不正确，您还可以输入" + times + "次");
                    times--;
                }
            }
        } else {
            if (toStr(pattern).length() < 4) {
                lockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
                timer.start();
                tv_title.setText("至少连接4个点，请重试");
            } else if (password.equals(toStr(pattern))) {
                ActivityManage.finishSame(LockActivity.class);
            } else if (times == 0) {
                //登录状态解锁次数超限
                loginOut();
            } else {
                lockPatternView.setDisplayMode(LockPatternView.DisplayMode.Wrong);
                timer.start();
                tv_title.setText("密码不正确，您还可以输入" + times + "次");
                times--;
            }
        }
    }

    private void loginOut() {
        Intent intent = new Intent(this, LoginActivity.class);
        ActivityManage.finishOthers(LoginActivity.class);
        startActivity(intent);
        new LogoutHelper(this).getOut();
        ShareUtil.getInstance().getOut();
    }

    private String toStr(List<LockPatternView.Cell> pattern) {
        String str = "";

        for (int i = 0; i < pattern.size(); i++) {
            int row = pattern.get(i).getRow();
            int column = pattern.get(i).getColumn();
            switch (row) {
                case 0:
                    if (column == 0) {
                        str += "1";
                    } else if (column == 1) {
                        str += "2";
                    } else if (column == 2) {
                        str += "3";
                    }
                    break;
                case 1:
                    if (column == 0) {
                        str += "4";
                    } else if (column == 1) {
                        str += "5";
                    } else if (column == 2) {
                        str += "6";
                    }
                    break;
                case 2:
                    if (column == 0) {
                        str += "7";
                    } else if (column == 1) {
                        str += "8";
                    } else if (column == 2) {
                        str += "9";
                    }
                    break;
            }
        }
        return str;
    }

    private Dialog dialog;

    @Override
    public void onClick(View view) {
        String s = "忘记密码，您需要重新登录！";
        if (view.getId() == R.id.btn_forget) {
            dialog = new SimpleDialog(this, s, new SimpleDialog.OnClick() {
                @Override
                public void ok() {
                    dialog.dismiss();
                    loginOut();
                }

                @Override
                public void cancel() {
                    dialog.dismiss();
                }
            }, true);
            dialog.show();
        }
    }
}
